<?php

namespace app\admin\controller;

use app\admin\services\InstrumentBindLogService;
use app\admin\services\QuanGuanMemberService;
use think\App;

class InstrumentBindLogController extends Common implements ICurd
{

    /**
     * 用户列表
     */
    private $memberList;

    public function initialize()
    {
        parent::initialize();
    }

    public function index()
    {
        // TODO: Implement index() method.
        if (request()->isPost()) {
            $requestData = input('post.');
            $param['page'] = $requestData['page'] ?: 1;
            $param['pageSize'] = $requestData['limit'] ?: 10;
            $param['key'] = trim($requestData['key']) ?: "";
            // 需要查询的字段
            $field = "
                id,
                title,
                status,
                member_id,
                create_time,
                instrument_id,
                create_time
            ";
            $list = $this->getService()->list($param, $field);
            // 用户ids
            $memberIdArr = array_column($list['data'], 'member_id');
            $memberList = $this->setMemberList($memberIdArr);
            foreach ($list['data'] as &$item) {
                $item['member_id'] = $memberList[$item['member_id']]['name'];
                $item['create_time'] = date("Y-m-d H:i:s", $item['create_time']);
            }
            return $this->jsonList($list['data'], $list['total']);
        }
        return $this->fetch("instrument_bind_log/index");
    }

    public function create()
    {
        // TODO: Implement create() method.
        if (request()->isPost()) {
            $param = $this->mapping();
            $res = $this->getService()->save($param);
            if ($res) {
                return $this->jsonSuccess("index");
            }
            return $this->jsonError();
        }
        return $this->fetch("instrument_bind_log/create");
    }

    public function update($id = 0)
    {
        // TODO: Implement update() method.
        if (request()->isPost()) {
            $param = $this->mapping();
            $res = $this->getService()->save($param, $id);
            if ($res) {
                return $this->jsonSuccess("index");
            }
            return $this->jsonError();
        } else {
            $info = $this->getService()->getInfoById($id);
            $memberInfo = $this->setMemberList([$info['member_id']]);
            $info['member_name'] = $memberInfo[$info['member_id']]['name'];
            $info['create_time'] = date("Y-m-d H:i:s", $info['create_time']);
            $this->assign([
                'info' => json_encode($info, true),
                'infoArr' => $info,
            ]);
            return $this->fetch("instrument_bind_log/update");
        }
        return $this->jsonError();
    }

    public function remove()
    {
        // TODO: Implement remove() method.
        if (request()->isPost()) {
            $data = input('post.');
            $id = (int)$data['id'];
            $res = $this->getService()->save(['is_delete' => 2], $id);
            if ($res) {
                return $this->jsonSuccess("index");
            }
            return $this->jsonError();
        }
        return $this->jsonError();
    }

    public function detail()
    {
        // TODO: Implement detail() method.
    }

    public function setStatus()
    {
        // TODO: Implement setStatus() method.
        if (request()->isPost()) {
            $id = input('post.id', 0, 'int');
            $status = input('post.status', 0, 'int');
            $res = $this->getService()->save([
                'status' => $status
            ], $id);
            if ($res) {
                return $this->jsonSuccess('index');
            }
            return $this->jsonError();
        }
        return $this->jsonError();
    }

    private function getService()
    {
        return InstrumentBindLogService::makeInstance();
    }

    private function mapping()
    {
        $data = input('post.');
        $param = [
            'title' => trim($data['title']) ?: "",
            'status' => (int)$data['status'] ?: 1,
            'sort' => (int)$data['sort'] ?: 0
        ];
        return $param;
    }

    /**
     * @return mixed
     */
    public function setMemberList($instrumentIdArr = [])
    {
        $memberService = QuanGuanMemberService::makeInstance();
        $memberList = $memberService->getDatasByIds($instrumentIdArr, "id,name");
        return $memberList;
    }


}